//
//  WHContactsDetailController.m
//  AddressBook
//
//  Created by on 2020/7/28.
//  Copyright © 2020 wh. All rights reserved.
//

#import "WHContactsDetailController.h"
#import "WHContactsDetailTableView.h"
#import "MacroDefinition.h"
#import "WHContactsEditController.h"
#import "DBHelper.h"
#import "WHHeaderView.h"



@interface WHContactsDetailController ()<WHContactsDetailTableViewDelegate>

@property (nonatomic, strong) WHContactsDetailTableView *detailTableview;
@property (nonatomic, strong) WHHeaderView *headerView;
@property (nonatomic, assign) CGFloat headerViewHeight;

@end

@implementation WHContactsDetailController
{
    NSString *_name;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initWithView];
    self.view.backgroundColor = UIColor.whiteColor;
    [super setRightBarButtonItemTitle:@"编辑"];
    [super setLefttBarButtonItemTitle:@"返回"];
    self.headerViewHeight = kMAXHEIGHT;
}

- (void)initWithView
{
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.detailTableview];
}

- (void)searchDataWithName:(NSString *)name
{
    _name = name;
    __weak typeof(self) weakSelf = self;
    [DBHelper searchWithName:_name
                successBlock:^(NSMutableArray * _Nonnull arr) {
                    [weakSelf.detailTableview setContentWithArr:arr];
        [weakSelf.headerView setDataWithImageStr:arr[3]?arr[3]:@"" nameStr:arr[0]?arr[0]:@""];
                }
                failureBlock:^(NSString * _Nonnull str) {
                    NSLog(@"%@",str);
                }];
}

- (void)viewWillAppear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}

- (void)viewWillDisappear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}

- (void)rightButtonAction
{
    WHContactsEditController *editVC = [[WHContactsEditController alloc] init];
    [editVC searchDataWithName:_name];
    [self.navigationController pushViewController:editVC animated:NO];
}

- (void)leftButtonAction
{
    [self.navigationController popViewControllerAnimated:NO];
}
#pragma mark - WHTableViewHeaderDelegate
//滑动时触发的事件
- (void)detailTableGetScrollContentOffsetY:(CGFloat)offsetY
{
    if (offsetY > 0) {
        // 上滑 headerView 高度变小 最小高度为 KMINHEIGHT
        if (self.headerViewHeight > KMINHEIGHT) {
              self.headerViewHeight -= offsetY;
          }
    }
    if (offsetY < 0) {
        //下滑 headerView 高度变大
        if (self.headerViewHeight < kMAXHEIGHT) {
            self.headerViewHeight -= offsetY;
        }
    }
    [self setViewFrame];
}

//根据中间值 KMIDDLEHEIGHT 判断停止滑动时 是到最大值 还是最小值
- (void)detailTableStopScrollAction
{
    if (self.headerViewHeight > KMIDDLEHEIGHT) {
        self.headerViewHeight = kMAXHEIGHT;
    }else if (self.headerViewHeight < KMIDDLEHEIGHT){
        self.headerViewHeight = KMINHEIGHT;
    }
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.1 animations:^{
        [weakSelf setViewFrame];
    }];
}

//改变headerView和tableView的高度和位置
- (void)setViewFrame
{
    self.headerView.frame = CGRectMake(0, 0, ScreenWidth, self.headerViewHeight);
    self.detailTableview.frame = CGRectMake(0, self.headerViewHeight, ScreenWidth, ScreenHeight-self.headerViewHeight);
}
#pragma mark - 懒加载
- (WHContactsDetailTableView *)detailTableview
{
    if (!_detailTableview) {
        CGRect detailTableViewFrame = CGRectMake(0, self.headerViewHeight, ScreenWidth, ScreenHeight-self.headerViewHeight);
        _detailTableview = [[WHContactsDetailTableView alloc] initWithFrame:detailTableViewFrame
                                                                      style:UITableViewStylePlain];
        _detailTableview.detailTableDelegate = self;
   }
    return _detailTableview;
}

- (WHHeaderView *)headerView
{
    if (!_headerView) {
        _headerView = [[WHHeaderView alloc] init];
        _headerView.frame = CGRectMake(0, 0, ScreenWidth, self.headerViewHeight);
    }
    return _headerView;
}

@end
